package com.mcxx.modules.temporary.fundpay.service.write;

import com.google.common.collect.Lists;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayFamilyEntity;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayMemberEntity;
import com.mcxx.modules.temporary.fundpay.dao.write.TmpFundPayFamilyWriteDao;
import com.mcxx.modules.temporary.fundpay.dto.TmpFundPayDTO;
import com.mcxx.modules.temporary.fundpay.entity.TmpFundPayFamilyEntity;
import com.mcxx.modules.temporary.fundpay.entity.TmpFundPayMemberEntity;
import com.mcxx.modules.temporary.fundpay.service.read.TmpFundPayFamilyReadService;
import com.mcxx.modules.temporary.fundpay.service.read.TmpFundPayMemberReadService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@TaTransactional
public class TmpFundPayFamilyWriteService {

    @Autowired
    private TmpFundPayFamilyWriteDao tmpFundPayFamilyWriteDao;
    @Autowired
    private TmpFundPayFamilyReadService tmpFundPayFamilyReadService;
    @Autowired
    private TmpFundPayMemberReadService tmpFundPayMemberReadService;
    @Autowired
    private TmpFundPayFamilyWriteService tmpFundPayFamilyWriteService;
    @Autowired
    private TmpFundPayMemberWriteService tmpFundPayMemberWriteService;


    public int insert(TmpFundPayFamilyEntity tmpFundPayFamily) {
    	return tmpFundPayFamilyWriteDao.insert(tmpFundPayFamily);
    }


    public int inserts(List<TmpFundPayFamilyEntity> tmpFundPayFamily){
        return tmpFundPayFamilyWriteDao.inserts(tmpFundPayFamily);
    }


    public int insertIgnoreNull(TmpFundPayFamilyEntity tmpFundPayFamily) {
    	return tmpFundPayFamilyWriteDao.insertIgnoreNull(tmpFundPayFamily);
    }


    public int update(TmpFundPayFamilyEntity tmpFundPayFamily) {
    	return tmpFundPayFamilyWriteDao.update(tmpFundPayFamily);
    }


    public int updateIgnoreNull(TmpFundPayFamilyEntity tmpFundPayFamily) {
    	return tmpFundPayFamilyWriteDao.updateIgnoreNull(tmpFundPayFamily);
    }


    public int deleteByFundPayIdAndFamilyId(String fundPayId, List<String> familyList) {
        return tmpFundPayFamilyWriteDao.deleteByFundPayIdAndFamilyId(fundPayId,familyList);
    }


    public void saveCapitalFamilyByOrderId(List<String> orderIdList, TmpFundPayDTO tmpFundPayDTO){
        if(orderIdList.size() <= 0){
            return;
        }

        List<TmpFundPayFamilyEntity> fundPayFamilyList = tmpFundPayFamilyReadService.getSaveCapitalFamilyByOrderId(orderIdList);


        List<TmpFundPayMemberEntity> fundPayMemberList = tmpFundPayMemberReadService.getCapitalMemberList(fundPayFamilyList);


        this.perfectFundPayInfo(fundPayFamilyList, fundPayMemberList, tmpFundPayDTO.getId(), tmpFundPayDTO.getPayDate());


        this.insertFamilyMember(fundPayFamilyList, fundPayMemberList);
    }

    public void insertFamilyMember(List<TmpFundPayFamilyEntity> fundPayFamilyEntityList,  List<TmpFundPayMemberEntity> fundPayMemberEntityList){

        int rst = 0;
        List<List<TmpFundPayFamilyEntity>> groupFundPayFamilyEntityList = Lists.partition(fundPayFamilyEntityList, 500);
        for (List<TmpFundPayFamilyEntity> familyEntity: groupFundPayFamilyEntityList) {
            rst = tmpFundPayFamilyWriteService.inserts(familyEntity);
            if(rst <= 0){
                throw new BizException("-1","临时救助名册家庭添加失败");
            }
        }


        List<List<TmpFundPayMemberEntity>> groupFundPayMemberEntityList = Lists.partition(fundPayMemberEntityList, 500);
        for(List<TmpFundPayMemberEntity> memberEntityList : groupFundPayMemberEntityList){
            rst = tmpFundPayMemberWriteService.inserts(memberEntityList);
            if(rst <= 0){
                throw new BizException("-1","临时救助名册成员添加失败");
            }
        }
    }


    public void perfectFundPayInfo( List<TmpFundPayFamilyEntity> fundPayFamilyList,
                                    List<TmpFundPayMemberEntity> fundPayMemberList,
                                    String fundPayId,
                                    Date payDate){
        Date createDate = new Date();
        Map<String, String> familyFundPayFamilyIdMap = new HashMap<>();

        for (TmpFundPayFamilyEntity fundPayFamilyEntity : fundPayFamilyList) {

            fundPayFamilyEntity.setId(Constant.getUUID());

            fundPayFamilyEntity.setFundPayId(fundPayId);

            fundPayFamilyEntity.setPayDate(payDate);

            fundPayFamilyEntity.setCreateTime(createDate);

            familyFundPayFamilyIdMap.put(fundPayFamilyEntity.getFamilyId(), fundPayFamilyEntity.getId());
        }

        for (TmpFundPayMemberEntity fundMemberEntity : fundPayMemberList) {

            fundMemberEntity.setId(Constant.getUUID());

            fundMemberEntity.setFundPayId(fundPayId);

            fundMemberEntity.setPayDate(payDate);

            fundMemberEntity.setCreateTime(createDate);

            fundMemberEntity.setFundPayFamilyId(familyFundPayFamilyIdMap.get(fundMemberEntity.getFamilyId()));
        }
    }
}
